package com.sh.business.listener;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.sh.business.config.RabbitmqSender;
import com.sh.business.domain.User;
import com.sh.business.domain.UserOutInvest;
import com.sh.business.domain.vo.BizUserImportVo;
import com.sh.business.domain.vo.BizUserOutInvestImportVo;
import com.sh.business.mapper.UserMapper;
import com.sh.business.mapper.UserOutInvestMapper;
import com.sh.common.core.enums.OrderTypeEnum;
import com.sh.common.core.exception.ServiceException;
import com.sh.common.core.utils.OrderSnUtil;
import com.sh.common.core.utils.SpringUtils;
import com.sh.common.core.utils.StringUtils;
import com.sh.common.excel.core.ExcelListener;
import com.sh.common.excel.core.ExcelResult;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author ruoyi
 */
@Slf4j
public class BizUserOutInvestImportListener extends AnalysisEventListener<BizUserOutInvestImportVo> implements ExcelListener<BizUserOutInvestImportVo> {

    private final UserMapper userMapper;
    private final UserOutInvestMapper userOutInvestMapper;
    private final RabbitmqSender rabbitMqSender;


    private int autoIncrement = 0;
    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public BizUserOutInvestImportListener() {
        this.userMapper = SpringUtils.getBean(UserMapper.class);;
        this.userOutInvestMapper = SpringUtils.getBean(UserOutInvestMapper.class);
        this.rabbitMqSender = SpringUtils.getBean(RabbitmqSender.class);
    }


    @Override
    public void invoke(BizUserOutInvestImportVo userVo, AnalysisContext context) {

        User user = userMapper.queryByWalletAddress(userVo.getWalletAddress());
        if (ObjectUtil.isNull(user)) {
            log.error("用户不存在:{}", userVo.getWalletAddress());
            return;
        }
        BigDecimal amount = NumberUtil.div(NumberUtil.sub(user.getComputility(), user.getPurchaseComputility()), BigDecimal.TEN);
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        UserOutInvest outInvest = new UserOutInvest();
        outInvest.setOrderSn(OrderSnUtil.generateOrderSn(OrderTypeEnum.WB));
        outInvest.setUserId(user.getUserId());
        outInvest.setAmount(amount);
        outInvest.setFromAddress(userVo.getWalletAddress());

        userOutInvestMapper.insert(outInvest);
        // 每次延迟3秒
        autoIncrement++;

        rabbitMqSender.sendDailyDynamicsOutTtlMsg(outInvest.getOrderSn(), autoIncrement * 3 * 1000L);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<BizUserOutInvestImportVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<BizUserOutInvestImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
